'''
Created on 2011-12-12

@author: latios
'''

import csv
import re
import datetime

TIME_FUZZ=0.5

class C:
    FILENAME=1
    SIM=6
    SIA=7
    SIC=8
    SIE=9
    FNM=10
    FNA=11
    FNC=12
    FNE=13 
    timere = re.compile(r'(\d+)/(\d+)/(\d+)_(\d+):(\d+):(\d+)(\.(\d+))?')
    @staticmethod
    def toTime(str):
        r = C.timere.match(str)
        if r==None:return None
        r = list(r.groups())
        del r[6]
        tmp = []
        for x in r:
            if (x == None):
                tmp.append(0)
            else :
                tmp.append(int(x))
        r = tmp
        if (r[0] < datetime.MINYEAR) :return datetime.datetime.min
        if (r[0] > datetime.MAXYEAR) :return datetime.datetime.max
        return datetime.datetime(r[0], r[1], r[2], r[3], r[4], r[5], r[6])



class Dict(dict):
   def __init__(self,default=0):
      self.default=default
   def __getitem__(self,key):
      self.setdefault(key, 0)
      return dict.__getitem__(self,key)

def getTimes(row):
    ret=[]
    ret.append(C.toTime(row[C.SIM]))
    ret.append(C.toTime(row[C.SIA]))
    ret.append(C.toTime(row[C.SIC]))
    ret.append(C.toTime(row[C.SIE]))
    ret.append(C.toTime(row[C.FNM]))
    ret.append(C.toTime(row[C.FNA]))
    ret.append(C.toTime(row[C.FNC]))
    ret.append(C.toTime(row[C.FNE]))
    return ret



counter={}
dcounter={}

filesortd=Dict()
foldersortd=Dict()
total_file=0
total_folder=0

def toBits(i):
   ret=[]
   mask = 1
   while mask < 2**8:
      yield mask & i == mask
      mask *=2
   

def getOrder(timeidx):
    orders=[]
    cur=0
    for i in range(timeidx.__len__()-1):
        orders.append(cur)
        pre,nxt=timeidx[i],timeidx[i+1]
        if (abs((nxt[0]-pre[0]).total_seconds())>TIME_FUZZ):
            cur+=1
    orders.append(cur)
    idxs=[i[1] for i in timeidx]
    return zip(idxs,orders)

def do_sort(row):
    global total_file,total_folder
    times = getTimes(row)
    if None in times :return 
    i2s = 'sim sia sic sie fnm fna fnc fne'.split()
    isdir = row[C.FILENAME][-1]=='\\'
    if isdir:
        total_folder+=1 
    else :total_file+=1
    for i in range(1,2**8):
        idx=0
        timeidx=[]
        for mask in toBits(i):
            if (mask):
                timeidx.append((times[idx],idx))
            idx+=1
        if (len(timeidx)<2):continue
        timeidx.sort()
        orders=getOrder(timeidx)
        orders=str(orders)
        #print orders
        if isdir:
            foldersortd[orders]+=1
        else:filesortd[orders]+=1
def do_count(row):
    times = getTimes(row)
    i2s = 'sim sia sic sie fnm fna fnc fne'.split()
    isdir = row[C.FILENAME][-1]=='\\'
    global counter
    global dcounter
    if (isdir):
        theC = dcounter
    else :theC = counter
    for i in range(8):
        for j in range(8):
            if times[j]==None or times[i]==None:continue
            if (times[i]>times[j]):
                if (i,j) in theC:
                    theC[(i,j)]+=1
                else :
                    theC[(i,j)]=1
i2s='sim sia sic sie fnm fna fnc fne'.split()
def toStringRep(sortd):
    ret=[]
    
    for i in sortd:
        lst=eval(i[0])
        strlst=[]
        rate=i[1]
        for (idx,order) in lst:
            strlst.append((i2s[idx],order))
        ret.append((strlst,rate))
    return ret
        
              
def main():
    import os
    import sys
    print 'arguments are ', sys.argv
    try :
        with open(sys.argv[1],'r') as f:
            reader = csv.reader(f)
            cur=0
            for row in reader:
                cur+=1
                #do_count(row)
                do_sort(row)
                if cur % 1000==0:
                    print >>sys.stderr,'progress -----%d-----\r' % cur,
    except KeyboardInterrupt:
        pass
    finally:
        def cmp(x,y):
            return y[1]-x[1] 
        print 'there are %d folders and %d files' %(total_folder,total_file)
        print 'regular file counter is ',counter
        print 'folder counter is ',dcounter
        global filesortd,foldersortd
        filesortd=sorted(filesortd.items(),cmp)
        foldersortd=sorted(foldersortd.items(),cmp)
        print 'regular file sort is ',filesortd
        print 'folder sort is ' , foldersortd
        print toStringRep(filesortd)
        print toStringRep(foldersortd) 
    
if __name__=='__main__':
    main()
    
    